package com.hsjk.szwj.module.smedia.service.job;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.GetSmartHandleJobRequest;
import com.aliyun.ice20201109.models.GetSmartHandleJobResponse;
import com.aliyun.ice20201109.models.SubmitASRJobRequest;
import com.aliyun.ice20201109.models.SubmitASRJobResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hsjk.szwj.module.smedia.controller.admin.job.vo.AsrJobReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.job.vo.AsrJobRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.job.vo.GetSmartHandleJobRespVO;
import com.hsjk.szwj.module.smedia.convert.job.JobConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * @author chenjiawei
 * @desc 智能任务相关接口实现类
 * @Date 2023/07/21 11:40
 **/
@Service
@Validated
@Slf4j
public class JobImpl implements IJob {

    @Value("${ali.media.access-key-id}")
    private String accessKeyId;

    @Value("${ali.media.access-key-secret}")
    private String accessKeySecret;

    @Value("${ali.media.endpoint}")
    private String endpoint;

    @Override
    public AsrJobRespVO submitASRJob(AsrJobReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        AsrJobRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            SubmitASRJobRequest request = new SubmitASRJobRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getDescription())) {
                request.setDescription(reqVO.getDescription());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getDuration())) {
                request.setDuration(reqVO.getDuration());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getStartTime())) {
                request.setStartTime(reqVO.getStartTime());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getTitle())) {
                request.setTitle(reqVO.getTitle());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getInputFile())) {
                request.setInputFile(reqVO.getInputFile());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getUserData())) {
                request.setUserData(reqVO.getUserData());
            }
            SubmitASRJobResponse jobResponse = client.submitASRJobWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(jobResponse)) {
                response = JobConvert.INSTANCE.convertAsrJobRespVO(jobResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public GetSmartHandleJobRespVO getSmartHandleJob(String jobId) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        GetSmartHandleJobRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            GetSmartHandleJobRequest request = new GetSmartHandleJobRequest();
            if (ObjectUtil.isNotEmpty(jobId)) {
                request.setJobId(jobId);
            }
            GetSmartHandleJobResponse withOptions = client.getSmartHandleJobWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(withOptions)) {
                response = JobConvert.INSTANCE.convertGetSmartHandleJobRespVO(withOptions.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }
}
